package com.blackline.auto.Variance;


import static org.junit.Assert.fail;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;


import com.blackline.auto.common.CommonMethods;
import com.blackline.declareUI.UI;
import com.blackline.parameters.Parameters;
import com.blackline.parameters.TestData;
import com.blackline.auto.common.*; 

public class Variance_smoketest {
	
	private WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private static Logger logger=Logger.getLogger(Variance_smoketest.class);
	SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMdd-kk-mm-ss");
	Date date = new Date();
	String dateString = sdf.format(date);
	UI UI=new UI();
	Parameters parameters;
	
	@Before
	public void setUp() throws Exception {
		FirefoxProfile profile=new FirefoxProfile();
		profile.setEnableNativeEvents(true);
		parameters=new Parameters("parameters.xml");
		driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		UI.declareAllUI(driver);
	      }
	
	
	
	
	@SuppressWarnings("static-access")
   @Test
	// ******Test Case 1: Verify the user can create a Variance;
	public void Variancesmoketest() throws Exception {
		
		TestData testdata=new TestData("TestData/Variance_Import.xml");
		
		//=======================Import Account=======================//
		SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMdd-kk-mm-ss");
		Date date = new Date();
		
		String dateString = sdf.format(date);
		logger.info("Current time is: " + dateString);
		//System.out.println("dateString :" +dateString);
		
		//The last field of following inputText is "8900"
		String inputText = "4000	"+ dateString +"									A2013022601	A2013022601	A	Asset	TRUE	TRUE	USD	USD	1/31/2011			8900";
		logger.info("The inputText for " + testdata.getInput("accountFile") + " is: " + inputText);
		//Create Account.Import.text file and write inputText into it.
		CommonMethods cm = new CommonMethods();
		cm.createFile(("TestData\\" + testdata.getInput("accountFile")), inputText);
		
		//The absolute path of the Account.Import.Auto01.txt file.
		String accountFilePath = System.getProperty("user.dir") + "\\TestData\\" + testdata.getInput("accountFile");
		logger.info("Location of " + testdata.getInput("accountFile") + " is: " + accountFilePath);
		
		//Account file without extension: Account.Import.Auto01.txt > Account.Import.Auto01
		String inputFileNoExtension = testdata.getInput("accountFile").substring(0, testdata.getInput("accountFile").length()-4);
		logger.info("Account file without extension is: " + inputFileNoExtension);
		
		//=======================Import Account=======================//
		int sleepTime = 5000;
	    UI.LogonPage.Login(driver, parameters.urlDev3, parameters.getLogonID("admin"), parameters.getPassword("admin"));
		UI.NavigationMenu.IMPORTS.click();
		UI.NavigationMenu.Import_Data.click();
		Thread.sleep(sleepTime);
		//verificationErrors.append(UI.page.VerifyPageTitle(driver, "Import Data from a file"));
		UI.page.selectOptionForDropdownList(UI.ImportData.Import_dropDownList, "Accounts");
	    UI.ImportData.Document_textField.sendKeys(accountFilePath);
		UI.ImportData.Import_button.click();
		Thread.sleep(sleepTime);
		
		String result = UI.ImportData.OperationResult.getText();
		logger.info(result);
		if (!result.contains("Fail")) //Expected msg: Operation Failed: Incorrect DAI Configuration
		{
		logger.info("Result is as expected: 'Operation Succeeded'.");
		}
		else
		{
			verificationErrors.append("Result is NOT as expected. Should be 'Operation Succeeded', but is: Failed");
		}
	
	  
	  	UI.NavigationMenu.VARIANCE.click();
		UI.NavigationMenu.Adhoc_Variance_Report.click();
		UI.page.selectOptionForDropdownList(UI.AdhocVariance.Adhoc_Period, "1/31/2011");
		UI.page.selectOptionForDropdownList(UI.AdhocVariance.Adhoc_Over, "2/29/2012");
		UI.AdhocVariance.Adhoc_percent.sendKeys("0");
		UI.AdhocVariance.Adhoc_amount.sendKeys("0");
		Thread.sleep(2000);
		UI.AdhocVariance.Adhoc_refresh.click();
		UI.AdhocVariance.Adhoc_acctnum.sendKeys(dateString);
		UI.AdhocVariance.Adhoc_apply.click();
		Thread.sleep(sleepTime);
		UI.AdhocVariance.Variance_checkbox.click();
		UI.AdhocVariance.Ad_Variance_analysis.click();
		String status = UI.AdhocVariance.Variance_Success_Message.getText();
		Thread.sleep(sleepTime);
		  if ("Variance Analysis Created/Updated: 1".equals(status))
		  {
		  logger.info("Status is as expectation: " + status);
		  }
		  else
		  {
			  verificationErrors.append("[Fail!] The Variance was not successfully created & The message is: " + status);
		  }
		  UI.SessionMenu.LogOut.click();
		  				
	}
	
	
	// ******Test Case 2: Verify the page "Variance Analysis Status" will be shown;
	@Test
	public void testVarianceAnalysisstatus() throws Exception {
	
	UI.LogonPage.Login(driver, parameters.urlDev3, parameters.getLogonID("admin"), parameters.getPassword("admin"));
	UI.NavigationMenu.VARIANCE.click();
    UI.NavigationMenu.Variance_Analysis.click();
    UI.page.VerifyPageTitle(driver, "Variance Analysis Status");    
  	}
	
	
	
	
	// ******Test Case 3: Verify the page "Variance Analysis Status" will be shown;
	@Test
	public void testBulkAssignment_Variance() throws Exception {
	
	UI.LogonPage.Login(driver, parameters.urlDev3, parameters.getLogonID("admin"), parameters.getPassword("admin"));
	UI.NavigationMenu.VARIANCE.click();
    UI.NavigationMenu.Variance_Analysis.click();
    UI.page.VerifyPageTitle(driver, "Variance Analysis Status");    
    UI.SessionMenu.LogOut.click();
  	}
	
	
	
	// ******Test Case 4: Verify that Variances can be  "BulkAssignment to preparer " ;
	@Test 
	public void testBulkAssignment_prep() throws Exception {
		
 
	UI.LogonPage.Login(driver, parameters.urlDev3, parameters.getLogonID("admin"), parameters.getPassword("admin"));
	UI.NavigationMenu.VARIANCE.click();
	UI.NavigationMenu.Bulk_Assignment_Variance.click();
	UI.BulkAssignment_Variance.BulkAssign_refresh.click();
	//UI.BulkAssignment_Variance.BulkAssign_acctnum.sendKeys("20130312-15-41-40");
	UI.BulkAssignment_Variance.BulkAssign_acctnum.sendKeys(dateString);
	UI.BulkAssignment_Variance.BulkAssign_apply.click();
	UI.BulkAssignment_Variance.BulkAssign_checkbox.click();
	Thread.sleep(2000);
	((JavascriptExecutor) driver).executeScript("document.getElementById('ctl00_ctl00_contentBody_cphMain_ddlRoleFrom_Input').removeAttribute('readonly');");
	//UI.BulkAssignment_Variance.BulkAssign_role.clear();
	Thread.sleep(2000);
	UI.BulkAssignment_Variance.BulkAssign_role.click();
	UI.BulkAssignment_Variance.BulkAssign_role_Value.click();
	//driver.findElement(By.cssSelector("#ctl00_ctl00_contentBody_cphMain_ddlRoleFrom_DropDown .rcbItem:nth-child(2)")).click();
			
	UI.BulkAssignment_Variance.BulkAssign_From.click();
	UI.BulkAssignment_Variance.BulkAssign_From_value.click();
	//driver.findElement(By.cssSelector("#ctl00_ctl00_contentBody_cphMain_ucbUserFrom_DropDown .rcbItem:nth-child(1)")).click();
	
	UI.BulkAssignment_Variance.BulkAssign_To.click();
	UI.BulkAssignment_Variance.BulkAssign_To_Value.click();
	// driver.findElement(By.cssSelector("#ctl00_ctl00_contentBody_cphMain_ucbUserTo_DropDown .rcbItem:nth-child(2)")).click();
   
	Thread.sleep(2000);
	UI.BulkAssignment_Variance.BulkAssign_refresh.click();
	UI.BulkAssignment_Variance.BulkAssign_checkbox.click();
	//UI.BulkAssignment_Variance.BulkAssign_From.clear();
	//UI.page.selectOptionForDropdownList(UI.BulkAssignment_Variance.BulkAssign_From, "Tucker, Therese");
	UI.BulkAssignment_Variance.BulkAssign_assignvarian.click();		
	String status = UI.BulkAssignment_Variance.BulkAssign_Successmsg.getText(); 

	Thread.sleep(2000);
	  if ("Success: Bulk assignment complete.  Assigned 1 variance analysis record.".equals(status))
	  {
	  logger.info("Status is as expectation: " + status);
	  }
	  else
	  {
		  verificationErrors.append("[Fail!] The Variance was not successfully created & The message is: " + status);
	  }
	  UI.SessionMenu.LogOut.click();
		
	}
	
	
	// ******Test Case 5: Verify the preparer is able to certify the variance;
		@Test
		public void testBulkAssignment_Certify() throws Exception {
		
		UI.LogonPage.Login(driver, parameters.urlDev3, parameters.getLogonID("preparer"), parameters.getPassword("preparer"));
		UI.NavigationMenu.VARIANCE.click();
		UI.BulkAssignment_Variance.BulkAssign_acctnum.sendKeys("20130312-15-41-40");
		UI.BulkAssignment_Variance.BulkAssign_acctnum.sendKeys(dateString);
		UI.BulkAssignment_Variance.BulkAssign_apply.click();
		UI.VarainceAnal_stats.Variance_edit.click();
		UI.VarainceAnal_stats.Variance_certify.click();
		UI.VarainceAnal_stats.Variance_certifybtn.click();
		}
		
		
		@After
		public void tearDown() throws Exception {
			CommonMethods.takeScreenShot(this.getClass(), driver);
			driver.close();
			driver.quit();
			String verificationErrorString = verificationErrors.toString();
			if (!"".equals(verificationErrorString)) {
				fail(verificationErrorString);
			}
		}
}

